// Game.cpp: implementation of the CGame class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Game.h"
#include "stdio.h"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern CGame game;
int i,j;
void on_mouse (int event, int x, int y, int flags, void* param)
{
	switch(event)
	{
	case CV_EVENT_LBUTTONDOWN:
		{

			if (x>185 &&  x<240  && y>310  && y<340)
			{
				cvShowImage("game",game.m_showImage);
			}
		}
	}
}

void on_mousepf (int event, int x, int y, int flags, void* param)
{
	if ((x-game.m_playerfish.m_px)*game.m_playerfish.m_direction>=0)
	{
		
	} 
	else
	{
		cvFlip(game.m_playerfish.m_playerImg,NULL,1);
		game.m_playerfish.m_direction=-	game.m_playerfish.m_direction;
	}
	switch(event)
	{
	case CV_EVENT_MOUSEMOVE:
		{
			if (x<game.m_playerfish.m_px)
			{
				
			}
			if (x>game.m_playerfish.m_px)
			{
				
			}
			if (x>=0 && 
				x<=(game.m_bkImage->width-game.m_playerfish.m_playerImg->width) 
				&& 
				y>0 
				&&y<=(game.m_bkImage->height-game.m_playerfish.m_playerImg->height) )
			{
				game.m_playerfish.m_px=x;
				game.m_playerfish.m_py=y;
			} 
			if(x>(game.m_bkImage->width-game.m_playerfish.m_playerImg->width) 
				&& x<(game.m_bkImage->width) 
				&& 
				y>0 
				&&y<=(game.m_bkImage->height-game.m_playerfish.m_playerImg->height) )
			{
				game.m_playerfish.m_px=game.m_bkImage->width-game.m_playerfish.m_playerImg->width;
				game.m_playerfish.m_py=y;
			}
			if(x>=0 && 
				x<=(game.m_bkImage->width-game.m_playerfish.m_playerImg->width) 
				&& y>(game.m_bkImage->height-game.m_playerfish.m_playerImg->height) 
				&& y<(game.m_bkImage->height) )
				
			{
				game.m_playerfish.m_px=x;
				game.m_playerfish.m_py=(game.m_bkImage->height-game.m_playerfish.m_playerImg->height) ;
			}
			
			if (x>(game.m_bkImage->width-game.m_playerfish.m_playerImg->width) 
				&& x<(game.m_bkImage->width) 
				&&y>(game.m_bkImage->height-game.m_playerfish.m_playerImg->height) 
				&& y<(game.m_bkImage->height))
			{
				game.m_playerfish.m_px=game.m_bkImage->width-game.m_playerfish.m_playerImg->width;
				
				game.m_playerfish.m_py=(game.m_bkImage->height-game.m_playerfish.m_playerImg->height) ;
			}
			break;
		}
		
	}
}

CGame::CGame()
{

}

CGame::~CGame()
{

}

void CGame::Initialize()
{

	m_StImage = cvLoadImage("star.jpg",1);
	m_bkImage = cvLoadImage("bk.jpg", 1);

	m_playerfish.Initialize();

	for (i = 0; i < 5; i++)
	{
		m_onefish[i].Initialize();
		m_twofish[i].Initialize();
		m_threefish[i].Initialize();

	}
	for (i = 0; i < 3; i++)
	{
		m_fourfish[i].Initialize();
		m_fivefish[i].Initialize();

	}
	m_showImage = cvCreateImage(cvGetSize(m_bkImage),
	m_bkImage->depth,m_bkImage->nChannels);

	cvCopy(m_bkImage,m_showImage);
	
	cvNamedWindow("game",1);
	cvShowImage("game",m_StImage);
	cvSetMouseCallback("game",on_mouse,0);
	m_playerfish.DrawToBack(m_showImage);
	for (i = 0; i < 5; i++)
	{
		m_onefish[i].DrawToBK(m_showImage);
		m_twofish[i].DrawToBK(m_showImage);
		m_threefish[i].DrawToBK(m_showImage);
	}
	for (i = 0; i < 3; i++)
	{
		m_fourfish[i].DrawToBK(m_showImage);
		m_fivefish[i].DrawToBK(m_showImage);
	}
	cvWaitKey(0);
}



void CGame::Run()
{
	cvCopy(m_bkImage,m_showImage);

	if (m_playerfish.m_num > 0 && m_playerfish.IsLive !=true )
	{
		m_playerfish.m_num--;
		m_playerfish.IsLive = true;
		m_playerfish.m_px = 0;
		m_playerfish.m_py = 0;
		
	}
	m_playerfish.DrawToBack(m_showImage);

	if (m_playerfish.m_score >= 20)
	{
		m_playerfish.m_barrier++;
		m_playerfish.m_score = 0;
		for (i = 0; i < 3; i++)
		{
			m_fivefish[i].Initialize();
			m_fourfish[i].Initialize();
			m_fourfish[i].m_aivx = m_fourfish[i].m_aivx - (m_playerfish.m_barrier - 1);
			m_fivefish[i].m_aivx = m_fivefish[i].m_aivx - (m_playerfish.m_barrier - 1);
		}
		for (i = 0 ; i < 5; i++)
		{
			m_onefish[i].Initialize();
			m_onefish[i].m_aivx = m_onefish[i].m_aivx - (m_playerfish.m_barrier - 1) * 2;
			m_twofish[i].Initialize();
			m_twofish[i].m_aivx = m_twofish[i].m_aivx - (m_playerfish.m_barrier - 1) * 2;
			m_threefish[i].Initialize();
			m_threefish[i].m_aivx = m_threefish[i].m_aivx - (m_playerfish.m_barrier - 1) * 2;
		}
	}
	for (i = 0; i < 5; i++)
	{	
		m_onefish[i].DrawToBK(m_showImage);
	    m_onefish[i].MoveAiFish();
		m_twofish[i].DrawToBK(m_showImage);
		m_twofish[i].MoveAiFish();
		m_threefish[i].DrawToBK(m_showImage);
		m_threefish[i].MoveAiFish();
		m_playerfish.EatSmallFish(&m_onefish[i]);
		m_playerfish.EatSmallFish(&m_twofish[i]);
		m_playerfish.EatSmallFish(&m_threefish[i]);
		if (m_onefish[i].IsAiLive != true)
		{
			m_onefish[i].Initialize();
			m_onefish[i].m_aivx = m_onefish[i].m_aivx - (m_playerfish.m_barrier - 1) * 2;
		}
		if (m_twofish[i].IsAiLive != true)
		{
			m_twofish[i].Initialize();
			m_twofish[i].m_aivx = m_twofish[i].m_aivx - (m_playerfish.m_barrier - 1) * 2;
		}
		if (m_threefish[i].IsAiLive !=true)
		{
			m_threefish[i].Initialize();
			m_threefish[i].m_aivx = m_threefish[i].m_aivx - (m_playerfish.m_barrier - 1) * 2;
		}
	}
	for (i = 0; i < 3; i++)
	{
		m_fourfish[i].DrawToBK(m_showImage);
		m_fourfish[i].MoveAiFish();
		m_fivefish[i].DrawToBK(m_showImage);
		m_fivefish[i].MoveAiFish();
		for (j = 0; j < 5; j++)
		{
			m_fourfish[i].EatPSFish(&m_playerfish,&m_onefish[j]);
			m_fourfish[i].EatPSFish(&m_playerfish,&m_twofish[j]);
			m_fourfish[i].EatPSFish(&m_playerfish,&m_threefish[j]);
			m_fivefish[i].EatPSFish(&m_playerfish,&m_onefish[j]);
			m_fivefish[i].EatPSFish(&m_playerfish,&m_twofish[j]);
			m_fivefish[i].EatPSFish(&m_playerfish,&m_threefish[j]);
		}
		for(j = 0; j < 3 ; j++)
		{
			m_fivefish[i].EatPSFish(&m_playerfish,&m_fourfish[j]);
			if(m_fourfish[j].IsAiLive != true)
			{
				m_fourfish[j].Initialize();
				m_fourfish[j].m_aivx = m_fourfish[j].m_aivx - (m_playerfish.m_barrier - 1);
			}
		}
	}
	char key = cvWaitKey(50);
	m_playerfish.KeyMove(key);
	cvSetMouseCallback("game",on_mousepf,0);
}

void CGame::ShowImage()
{
	cvShowImage("game",m_showImage);
}

